﻿/****************************** Module Header ******************************\
 * Module Name:  DbParams.cs
 * Module Version:  1.0.0 (2012-9-4)
 * Copyright (c) AnyEB.Com (14507247@qq.com)
 * 
 * This class is used to list the file type.
 * 
 * This source is subject to the GPL License.
 * See http://www.gnu.org/licenses/gpl.html.
 * All other rights reserved.
 * 
 * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
 * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED 
 * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
\***************************************************************************/
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Data;
using System.Linq;

namespace UCL.Data
{

    /// <summary>
    /// 数据库参数对象
    /// <para>提供存储过程参数的相关方法</para>
    /// <para>DbParams.New(DBHelper.GetDbCommand(providerName)).Add&lt;int&gt;("intOutCode").AddRange&lt;string&gt;(new string[] { "@loginid", "@password", "@ip" }, loginid, password, ip).AddRange&lt;int&gt;(new string[] { "type", "code" }, 5, 6).ToArrayDbParameters()</para>
    /// </summary>
    /// <typeparam name="K"></typeparam>
    /// <typeparam name="V"></typeparam>
    public class DbParameterx
    {
        /// <summary>
        /// 参数对象列表
        /// </summary>
        private IList<DbParameter> lstParams;
        /// <summary>
        /// 通用数据库命令对象
        /// </summary>
        private DbCommand cmd;

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="providerName"></param>
        public DbParameterx(string providerName = "System.Data.SqlClient")
        {
            IDatabaseHelper dbh = DatabaseHelperFactory.GetDatabaseHelper(providerName);
            if (null == dbh)
            {
                throw new Exception("数据库提供程序不受支持!");
            }
            cmd = dbh.GetDbCommand();
            lstParams = new List<DbParameter>();
        }

        /// <summary>
        /// 重置实例对象，清空所有参数对象
        /// </summary>
        public void Clear()
        {
            this.lstParams.Clear();
        }

        /// <summary>
        /// 添加一个参数
        /// </summary>
        /// <param name="act"></param>
        /// <returns></returns>
        /// <remarks>2015-1-29 by sutroon</remarks>
        public DbParameterx Add(Action<DbParameter> act)
        {
            DbParameter p = cmd.CreateParameter();
            act(p);
            this.lstParams.Add(p);
            return this;
        }

        /// <summary>
        /// 添加一个参数
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="name"></param>
        /// <param name="value"></param>
        /// <param name="act"></param>
        /// <returns></returns>
        /// <remarks>2015-1-28 by sutroon 重构,新增参数act</remarks>
        public DbParameterx Add<T>(string name, T value, Action<DbParameter> act)
        {
            DbParameter p = cmd.CreateParameter();
            p.ParameterName = name;
            p.Value = value;
            if (null != act)
            {
                act(p);
            }
            this.lstParams.Add(p);
            return this;
        }

        /// <summary>
        /// 添加一个传入参数
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="name"></param>
        /// <param name="value"></param>
        /// <param name="act"></param>
        /// <returns></returns>
        /// <remarks>2015-1-29 by sutroon</remarks>
        public DbParameterx AddInput<T>(string name, T value, Action<DbParameter> act = null)
        {
            DbParameter p = cmd.CreateParameter();
            p.ParameterName = name;
            p.Value = value;
            p.Direction = ParameterDirection.Input;
            if (null != act)
            {
                act(p);
            }
            this.lstParams.Add(p);
            return this;
        }

        /// <summary>
        /// 添加一个输出参数
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="name"></param>
        /// <param name="act"></param>
        /// <returns></returns>
        /// <remarks>2015-1-29 by sutroon</remarks>
        public DbParameterx AddOutput<T>(string name, Action<DbParameter> act = null)
        {
            DbParameter p = cmd.CreateParameter();
            p.ParameterName = name;
            p.Direction = ParameterDirection.Output;
            if (null != act)
            {
                act(p);
            }
            this.lstParams.Add(p);
            return this;
        }

        /// <summary>
        /// 批量添加输入参数
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="names"></param>
        /// <param name="values"></param>
        /// <param name="act"></param>
        /// <returns></returns>
        /// <remarks>2015-1-29 by sutroon</remarks>
        public DbParameterx AddInputParameters<T>(string[] names, string[] values, Action<DbParameter> act)
        {
            DbParameter p = cmd.CreateParameter();
            int i = 0;
            foreach (string name in names)
            {
                p.ParameterName = name;
                p.Value = values[i];
                p.Direction = ParameterDirection.Input;
                if (null != act)
                {
                    act(p);
                }
                this.lstParams.Add(p);
                i++;
            }
            return this;
        }

        /// <summary>
        /// 批量添加out参数
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="act"></param>
        /// <param name="names"></param>
        /// <returns></returns>
        /// <remarks>2015-1-29 by sutroon</remarks>
        public DbParameterx AddOutputParameters<T>(Action<DbParameter> act, params string[] names)
        {
            DbParameter p = cmd.CreateParameter();
            foreach (string name in names)
            {
                p.ParameterName = name;
                p.Direction = ParameterDirection.Output;
                if (null != act)
                {
                    act(p);
                }
                this.lstParams.Add(p);
            }
            return this;
        }

        /// <summary>
        /// 获取DbParameter格式的数组
        /// </summary>
        /// <returns></returns>
        public DbParameter[] ToArray()
        {
            return lstParams.ToArray();
        }
    }
}
